import collections
class Solution(object):
    def allCellsDistOrder(self, rows, cols, rCenter, cCenter):
        """
        :type rows: int
        :type cols: int
        :type rCenter: int
        :type cCenter: int
        :rtype: List[List[int]]
        """
        a_set = {(rCenter, cCenter)}
        out = []
        stack = collections.deque([(rCenter, cCenter)])
        while stack:
            x, y = stack.popleft()
            out.append([x, y])
            for ux, uy in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
                if 0 <= ux < rows and 0 <= uy < cols and (ux, uy) not in a_set:
                    a_set.add((ux, uy))
                    stack.append((ux, uy))
        return out
